package com.swz.dcrm.ui.car;


import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.CellInfo;
import com.bin.david.form.data.column.ArrayColumn;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.bg.BaseCellBackgroundFormat;
import com.bin.david.form.data.format.bg.ICellBackgroundFormat;
import com.bin.david.form.data.format.draw.IDrawFormat;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.table.ArrayTableData;
import com.bin.david.form.data.table.TableData;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Car;
import com.swz.dcrm.model.CarCompare;
import com.swz.dcrm.model.CarConfigTable;
import com.swz.dcrm.model.CarModel;
import com.swz.dcrm.model.CarParam;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;
import com.swz.dcrm.util.Tool;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class CarConfigFragment extends BaseFragment {

    @BindView(R.id.table)
    SmartTable smartTable;

    @Inject
    CarManagementViewModel carManagementViewModel;

    public static CarConfigFragment newInstance() {

        return new CarConfigFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        carManagementViewModel.getCurrentNeedCompareCar().observe(this,carModel -> {
            carManagementViewModel.getCarCompareConfig(carModel.getId())
                    .observe(this, new Observer<BaseResponse<CarCompare>>() {
                        @Override
                        public void onChanged(BaseResponse<CarCompare> carCompareBaseResponse) {
                            if(carCompareBaseResponse.isSuccess()){
                                if(carCompareBaseResponse.getData().getCarModelDetail().getId()!=carModel.getId()){
                                    return;
                                }

                                CarCompare carCompare=carCompareBaseResponse.getData();
/*
                                List<String> t=new ArrayList<>();
                                for(int i=0;i<=carCompare.getCompetingCarModelDetailList().size();i++){
                                    t.add("");
                                }
                                carCompare.getCompetingCarModelDetailList().add(carCompare.getCarModelDetail());
                                String[][] strings=new String[t.size()][carCompare.getCarModelDetail().getCarEnList().size()];
                                for(int i=0;i<carCompare.getCompetingCarModelDetailList().size();i++){
                                    strings[0][i]=carCompare.getCarModelDetail().getCarEnList().get(i).getParamType();

                                }
                                for(int i=1;i<t.size();i++){
                                    for(int k=0;k<carCompare.getCarModelDetail().getCarEnList().size();k++){
                                        strings[i][k]=carCompare.getCompetingCarModelDetailList().get(i).getCarEnList().get(k).getValue();
                                    }
                                }

                                ArrayTableData<String> tableData=ArrayTableData.create("竞品对比", t.toArray(new String[]{}), strings, new IDrawFormat<String>() {
                                    @Override
                                    public int measureWidth(Column<String> column, int position, TableConfig config) {
                                          return Tool.dip2px(getActivity(),90);
                                    }

                                    @Override
                                    public int measureHeight(Column<String> column, int position, TableConfig config) {
                                        return Tool.dip2px(getActivity(),44);
                                    }

                                    @Override
                                    public void draw(Canvas c, Rect rect, CellInfo<String> cellInfo, TableConfig config) {
                                        Paint paint = config.getPaint();
                                        if(cellInfo.row%2 == 0&&cellInfo.col!=0){
                                            paint.setColor(Color.parseColor("#f4f6f9"));
                                        }else if(cellInfo.row%2 != 0&&cellInfo.col!=0){
                                            paint.setColor(Color.parseColor("#eef1f6"));
                                        }else {
                                            paint.setColor(  Color.parseColor("#ffffff"));
                                        }
                                        //绘制正方形
                                        c.drawRect(rect.left+5,rect.top+5,rect.right-5,rect.bottom-5,paint);
                                        if(cellInfo.col==0){
                                            paint.setColor(ContextCompat.getColor(getContext(), R.color.blue_0f6eff));

                                        }else {
                                            paint.setColor(ContextCompat.getColor(getContext(),R.color.black_2f3234));
                                        }
                                        c.drawText(cellInfo.value,rect.right/2,rect.bottom/2,paint);
                                    }
                                });
                               smartTable.setTableData(tableData);*/
                            List<Column> columns=new ArrayList<>();
                            Column column=new Column("基本参数","name");
                            column.setFixed(true);
                            columns.add(column);
                            Column column1=new Column("车型","carParam.value");
                            columns.add(column1);

                            List<CarConfigTable>list=new ArrayList<>();

                            for(int i=1;i<=carCompare.getCompetingCarModelDetailList().size();i++){
                                Column column2=new Column("","carParam"+i+".value");
                                columns.add(column2);
                            }

                                for(int i=0;i<carCompare.getCarModelDetail().getCarEnList().size()-3;i++){


                                    CarConfigTable carConfigTable=new CarConfigTable();
                                    List<CarParam> params=new ArrayList<>();
                                    List<CarParam>brand=new ArrayList<>();
                                    String name=carCompare.getCarModelDetail().getCarEnList().get(i).getCnName();
                                    if(name.length()>15){
                                        name=name.substring(0,10)+"\n"+name.substring(10);
                                    }
                                    carConfigTable.setName(name);




                                    if(carCompare.getCarModelDetail().getCarEnList().get(i).getValue().length()>10){
                                        String s=carCompare.getCarModelDetail().getCarEnList().get(i).getValue().substring(0,carCompare.getCarModelDetail().getCarEnList().get(i).getValue().length()/2)+"\n"+
                                                carCompare.getCarModelDetail().getCarEnList().get(i).getValue().substring(carCompare.getCarModelDetail().getCarEnList().get(i).getValue().length()/2);
                                        carCompare.getCarModelDetail().getCarEnList().get(i).setValue( s);
                                    }
                                    carConfigTable.setCarParam(carCompare.getCarModelDetail().getCarEnList().get(i));

                                    for(int k=1;k<=carCompare.getCompetingCarModelDetailList().size();k++) {
                                        CarCompare.CarModelDetail c = carCompare.getCompetingCarModelDetailList().get(k-1);
                                        if (c.getCarEnList().get(i).getValue().length() > 20) {
                                            String s = c.getCarEnList().get(i).getValue().substring(0, c.getCarEnList().get(i).getValue().length() / 2) + "\n" +
                                                    c.getCarEnList().get(i).getValue().substring(c.getCarEnList().get(i).getValue().length() / 2);
                                            c.getCarEnList().get(i).setValue(s);
                                        }
                                        if (k == 1) {
                                            carConfigTable.setCarParam1(c.getCarEnList().get(i));
                                        } else if (k == 2) {
                                            carConfigTable.setCarParam2(c.getCarEnList().get(i));
                                        } else if (k == 3) {
                                            carConfigTable.setCarParam3(c.getCarEnList().get(i));
                                        } else if (k == 4) {
                                            carConfigTable.setCarParam4(c.getCarEnList().get(i));
                                        }
                                    }
                                    list.add(carConfigTable);
                                }

                             smartTable.setTableData(new TableData("对比",list,columns));




                            }
                        }
                    });
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_car_config;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        TableConfig tableConfig= smartTable.getConfig();
        tableConfig.setShowXSequence(false);
        tableConfig.setShowYSequence(false);
        tableConfig.setShowColumnTitle(false);
        smartTable.setZoom(true);
        FontStyle fontStyle=new FontStyle();
        fontStyle.setTextSpSize(getContext(),12);
        tableConfig.setContentStyle(fontStyle);
        tableConfig.setShowTableTitle(false);
        tableConfig.setShowYSequence(false);
        tableConfig.setShowXSequence(false);



        ICellBackgroundFormat<CellInfo> backgroundFormat = new BaseCellBackgroundFormat<CellInfo>() {
            @Override
            public int getBackGroundColor(CellInfo cellInfo) {
                if(cellInfo.row%2 == 0&&cellInfo.col!=0){
                    return Color.parseColor("#f4f6f9");
                }else if(cellInfo.row%2 != 0&&cellInfo.col!=0){
                    return Color.parseColor("#eef1f6");
                }
                return Color.parseColor("#ffffff");
            }

            @Override
            public int getTextColor(CellInfo cellInfo) {
                if(cellInfo.col==0){
                    return ContextCompat.getColor(getContext(), R.color.blue_0f6eff);
                }
                return ContextCompat.getColor(getContext(),R.color.black_2f3234);
            }
        };
        tableConfig.setMinTableWidth(Tool.dip2px(getActivity(),90));
        tableConfig.setHorizontalPadding(0);
        tableConfig.setContentCellBackgroundFormat(backgroundFormat);
        tableConfig.setVerticalPadding(Tool.dip2px(getActivity(),17));
        return true;
    }



}
